package com.leyou.item.controller;

import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.entity.Category;
import com.leyou.item.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
//@CrossOrigin("http://manage.leyou.com")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 根据分类的父id查询分类列表
     * 接收form表单类型数据 @RequestParam
     * 接收json格式数据 @RequestBody
     * 接收rest风格参数 @PathVariable
     * 如果是feign要调用的处理器，即便是form格式数据，注解也不能省略
     */
    @GetMapping("/category/of/parent")
    public ResponseEntity<List<Category>> queryCategorysByPid(@RequestParam("pid") Long pid){
        List<Category> list = categoryService.queryCategorysByPid(pid);
        //return ResponseEntity.status(HttpStatus.OK).body(list);
        return ResponseEntity.ok(list);
    }

    /**
     * 根据分类id集合查询分类对象集合
     */
    @GetMapping("/category/list")
    public ResponseEntity<List<Category>> queryCategorysByIds(@RequestParam("ids") List<Long> ids){
        List<Category> list = categoryService.queryCategorysByIds(ids);
        return ResponseEntity.ok(list);
    }

    /**
     * 首页分类导航 查询全部分类
     */
    @GetMapping("/category/parent")
    public ResponseEntity<List<CategoryDTO>> queryCategoryDtoByPid(@RequestParam("pid") Long pid){
        List<CategoryDTO> list = categoryService.queryCategoryDtoByPid(pid);
        return ResponseEntity.ok(list);
    }

}
